﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmInventory : Form
    {
        public int pnum = -1;
        public frmInventory()
        {
            InitializeComponent();
        }
        public frmInventory(int id)
        {
            pnum = id;
            InitializeComponent();
        }
        private void btnEnter_Click(object sender, EventArgs e)
        {
            if (pnum == -1)
            {
                int pid = (int)cmbPname.SelectedValue;
                int num = int.Parse(txtNum.Text);
                string sq = "SELECT pid FROM inventory where Pid="+pid+" and did="+userinfo.id;
                DataSet da=DbHelperOleDb.Query(sq);
                string d = dtpDay.Value.ToShortDateString();
                string sql = string.Format("INSERT INTO ininventory ([Pid],[Innum],[inday],[did] ) VALUES({0},{1},#{2}#,{3})", pid, num,d,userinfo.id); 
                if (da.Tables[0].Rows.Count == 1)
                {
                   //int m = (int)da.Tables[0].Rows[0]["innum"];
                   string sql1 = string.Format("update inventory set [inum] = [inum]+{0} where [Pid] = {1} and did={2}",num,pid,userinfo.id);
                  int k = DbHelperOleDb.ExecuteSql(sql1);
                 }
                 else

                { 
                    string sql1 = string.Format("INSERT INTO inventory ([Pid] ,[inum],[did] ) VALUES ({0},{1},{2})",pid,num,userinfo.id);
                    int k = DbHelperOleDb.ExecuteSql(sql1);
                }              
                 
                int i = DbHelperOleDb.ExecuteSql(sql);

                if (i == 1)
                {
                    MessageBox.Show("产品" + cmbPname.Text + "，入库数量为" + num + "成功", "提示");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("入库失败！", "提示");
                }
            }
            else
            {
                //修改产品
                int n = int.Parse(txtNum.Text);
                string sql = string.Format("update inventory set [inum] = {0} where [Pid]={1}", n, pnum);
                int i = DbHelperOleDb.ExecuteSql(sql);
                if (i >= 1)
                {
                    MessageBox.Show("修改成功！");
                    this.Close();

                }
                else
                {
                    MessageBox.Show("修改失败！");
                }
            }
        }
        DataSet ds;
        private void frmInventory_Load(object sender, EventArgs e)
        {
            if (pnum != -1)
            {
                this.Text = "入库修改";
                string sql1 = "SELECT ProductInfo.PName, inventory.* FROM ProductInfo INNER JOIN inventory ON ProductInfo.PID = inventory.Pid where ProductInfo.Pid= " + pnum + " and did=" + userinfo.id;
                ds = DbHelperOleDb.Query(sql1);
                txtNum.Text = ds.Tables[0].Rows[0]["inum"].ToString();
                cmbPname.DataSource = ds.Tables[0];
                cmbPname.DisplayMember = "PName";
                cmbPname.ValueMember = "PID";
            }
            else
            {
                string sql = "select [PID],[PName] from ProductInfo";
                DataSet ds = DbHelperOleDb.Query(sql);
                cmbPname.DataSource = ds.Tables[0];
                cmbPname.DisplayMember = "PName";
                cmbPname.ValueMember = "PID";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
